package csel.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import csel.model.*;

/*
 * Provides the Panel interface which allows for a customizable refresh
 * rate for controlling the rendering speed/framerate.
 * @author Ryan
 */
public class AreaViewPanel extends JPanel implements Runnable {
	
	private int pWidth = 440;//set Panel width
	private int pHeight = 440;//set Panel height
	private final Thread animator; //animation thread
	private volatile boolean running = false; //stops animator

	public int desiredFrameRate = 60;//set fps
	
	private long period = (long)(1000.0/desiredFrameRate) * 1000000L; //periods between drawings

	private Model model;
	private VisibleTilesGetter vtg = new VisibleTilesGetter();
	private TileRenderer tr;
	private Tile t[][];
	private int count = 0;
	
	/*******************************************************************/
	private Timer timer;
	private final Object lock = new Object();
	/*******************************************************************/
	
	/**
	 * constructor
	 * @param Model m
	 */
	public AreaViewPanel(Model m)
	{
		model = m;
		tr = new TileRenderer(m.getMap());
		vtg = new VisibleTilesGetter();
		setBackground(Color.white); 
		setPreferredSize( new Dimension(pWidth, pHeight));
		
		setFocusable(true);
		requestFocus(); //JPanel now has focus, receives key presses
		animator = new Thread(this);
		
		/*******************************************************************/
		timer = new Timer(100, new Whip());
		/*******************************************************************/
	}
	
	/**
	 * initialize and start thread
	 */
	public void startLoop()
	{
		animator.start();
		
		/*******************************************************************/
		timer.start(); // This will have to be stopped!
		/*******************************************************************/
		
		System.out.println("[VIEW] started animator thread.");
	}
	
	/**
	 * Stops the game
	 */
	public void stopRenderer()
	{
		/*******************************************************************/
		timer.stop();
		/*******************************************************************/
		
		animator.interrupt();
	}
	
	/**
	 * Animator thread instantiation.
	 * Frames of animation are drawn in while loop
	 */
	/*******************************************************************/
	public void run()
	{
		System.out.println("[VIEW] Starting render loop");
		while (!Thread.interrupted())
		{
			repaint();
			try
			{
				synchronized (lock)
				{
					lock.wait();
				}
			} catch (InterruptedException ex)
			{
				Thread.currentThread().interrupt();
			}

		}
		System.out.println("[VIEW] Animator thread terminated!");
	}
	/*******************************************************************/
	
	/**
	 * All drawing rendered in this method
	 */
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		t = vtg.getVisibleTiles(model.getPlayerEntity().getLocation());
		tr.renderTiles(g, t);
		g.drawString(""+count++, 50, 50);
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}
	
	/*******************************************************************/
	// Timer Operation
	private class Whip implements ActionListener
	{
		// wake that bitch up
		public void actionPerformed(ActionEvent e)
		{
			synchronized( lock )
			{
				lock.notify();
			}
//			System.out.println("Go, you bastard!");
		}
	}
	/*******************************************************************/
    
   
}
